package com.bwie.commodity;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.GsonBuilder;
import com.bwie.commodity.domain.vo.ElectronicsVO;

import java.util.ArrayList;
import java.util.List;

public class ElectronicsApp {

    public static void main(String[] args) {
        List<ElectronicsVO> electronics = getElectronics();
//
//        List<ElectronicsVO> oneTree = new ArrayList<>();
//        electronics.forEach(electronicsVO -> {
//           if (electronicsVO.getPId() == 0){
//               oneTree.add(electronicsVO);
//           }
//        });
//        getTwoTree(oneTree);
////        oneTree.forEach(electronicsVO1 -> {
////            System.out.println(electronicsVO1);
////        });
//
//        System.out.println(oneTree);

        // 构建树形结构
        List<ElectronicsVO> tree = buildTree(electronics, 0);

        // 打印成JSON格式
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(tree);
        System.out.println(json);
    }


private static List<ElectronicsVO> buildTree(List<ElectronicsVO> list, int parentId) {
    List<ElectronicsVO> result = new ArrayList<>();
    for (ElectronicsVO item : list) {
        if (item.getPId().equals(parentId)) {
            item.setChildren(buildTree(list, item.getId()));
            result.add(item);
        }

    }
    return result;
}

//    private static void getTwoTree(List<ElectronicsVO> oneTree) {
//        List<ElectronicsVO> electronics = getElectronics();
//
////        oneTree.forEach(electronicsVO1 -> {
////           electronics.forEach(electronicsVO2 -> {
////              if (electronicsVO1.getId() == electronicsVO2.getPId()){
////                  twoTree.add(electronicsVO2);
////                  electronicsVO1.setChildren(twoTree);
////                  getTwoTree(twoTree);
////              }
////           });
////        });
//
//        electronics.forEach(electronicsVO -> {
//            List<ElectronicsVO> twoTree = new ArrayList<>();
//
//            oneTree.forEach(electronicsVO1 -> {
//               if (electronicsVO.getPId() == electronicsVO1.getId()){
//
//                   twoTree.add(electronicsVO);
//                   electronicsVO1.setChildren(twoTree);
//                   getTwoTree(twoTree);
//               }
//           });
//        });
//    }

    private static List<ElectronicsVO> getElectronics() {
        List<ElectronicsVO> list = new ArrayList<>();

        ElectronicsVO electronicsVO2 =new ElectronicsVO();
        electronicsVO2.setId(2);
        electronicsVO2.setPId(0);
        electronicsVO2.setElectronicsName("手机");
        list.add(electronicsVO2);

        ElectronicsVO electronicsVO3 =new ElectronicsVO();
        electronicsVO3.setId(3);
        electronicsVO3.setPId(0);
        electronicsVO3.setElectronicsName("电脑");
        list.add(electronicsVO3);

        ElectronicsVO electronicsVO4 =new ElectronicsVO();
        electronicsVO4.setId(4);
        electronicsVO4.setPId(2);
        electronicsVO4.setElectronicsName("苹果");
        list.add(electronicsVO4);

        ElectronicsVO electronicsVO5 =new ElectronicsVO();
        electronicsVO5.setId(5);
        electronicsVO5.setPId(2);
        electronicsVO5.setElectronicsName("小米");
        list.add(electronicsVO5);

        ElectronicsVO electronicsVO6 =new ElectronicsVO();
        electronicsVO6.setId(6);
        electronicsVO6.setPId(4);
        electronicsVO6.setElectronicsName("iphone正常版");
        list.add(electronicsVO6);

        ElectronicsVO electronicsVO12 =new ElectronicsVO();
        electronicsVO12.setId(12);
        electronicsVO12.setPId(4);
        electronicsVO12.setElectronicsName("iphoneMINI版");
        list.add(electronicsVO12);

        ElectronicsVO electronicsVO7 =new ElectronicsVO();
        electronicsVO7.setId(7);
        electronicsVO7.setPId(6);
        electronicsVO7.setElectronicsName("iphone16");
        list.add(electronicsVO7);

        ElectronicsVO electronicsVO13 =new ElectronicsVO();
        electronicsVO13.setId(13);
        electronicsVO13.setPId(12);
        electronicsVO13.setElectronicsName("iphone16 MINI");
        list.add(electronicsVO13);

        ElectronicsVO electronicsVO8 =new ElectronicsVO();
        electronicsVO8.setId(8);
        electronicsVO8.setPId(3);
        electronicsVO8.setElectronicsName("联想");
        list.add(electronicsVO8);

        ElectronicsVO electronicsVO9 =new ElectronicsVO();
        electronicsVO9.setId(9);
        electronicsVO9.setPId(8);
        electronicsVO9.setElectronicsName("办公本");
        list.add(electronicsVO9);

        ElectronicsVO electronicsVO10 =new ElectronicsVO();
        electronicsVO10.setId(10);
        electronicsVO10.setPId(8);
        electronicsVO10.setElectronicsName("游戏本");
        list.add(electronicsVO10);

        ElectronicsVO electronicsVO11 =new ElectronicsVO();
        electronicsVO11.setId(11);
        electronicsVO11.setPId(10);
        electronicsVO11.setElectronicsName("联想r9000p");
        list.add(electronicsVO11);

        return list;
    }


}
